package com.njuptB180316.boki.controller;

import com.njuptB180316.boki.dao.BookMapper;
import com.njuptB180316.boki.dao.RecordMapper;
import com.njuptB180316.boki.dao.UserMapper;
import com.njuptB180316.boki.dataobject.Book;
import com.njuptB180316.boki.dataobject.Record;
import com.njuptB180316.boki.dataobject.User;
import com.njuptB180316.boki.interceptor.NeedLogin;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class AdminController {
    @Autowired
    private BookMapper bookMapper;
    @Autowired
    private RecordMapper recordMapper;
    @Autowired
    private UserMapper userMapper;

    @NeedLogin
    @RequestMapping("/admin_book")
    public String adminBook(ModelMap modelMap, HttpServletRequest request){
        if (getUserAuthority(request)) return "redirect:/";
        List<Book> books = bookMapper.selectAll();
        modelMap.addAttribute("books", books);
        return "admin_book";
    }

    @NeedLogin
    @RequestMapping("/admin_record")
    public String adminBorrowRecord(ModelMap modelMap, HttpServletRequest request){
        if (getUserAuthority(request)) return "redirect:/";
        List<Record> records = recordMapper.selectAll();
        modelMap.addAttribute("records", records);
        return "admin_record";
    }

    private boolean getUserAuthority(HttpServletRequest request) {
        User user = (User) request.getSession().getAttribute("user");
        int authority = user.getAuthority();
        if (authority == 0) {
            return true;
        }
        return false;
    }

    @NeedLogin
    @RequestMapping("/delete_record")
    public String deleteRecord(@RequestParam("rphone") long recordPhone,
                               @RequestParam("rtime") String recordTime) {
        recordMapper.delete(recordPhone,Long.parseLong(recordTime));
        return "redirect:/admin_record";
    }

    @NeedLogin
    @RequestMapping("/adjust_book")
    public String adjustBook(@RequestParam("bid") int bookId,
                             @RequestParam("bname") String bookName,
                             @RequestParam("bauthor") String bookAuthor,
                             @RequestParam("bnum") int bookNum,
                             @RequestParam("bbrief") String bookBrief,
                             @RequestParam("burl") String bookUrl) {
        Book book = new Book();
        book.setId(bookId);
        book.setName(bookName);
        book.setAuthor(bookAuthor);
        book.setBrief(bookBrief);
        book.setNum(bookNum);
        book.setPicture(bookUrl);
        bookMapper.updateByPrimaryKey(book);
        return "redirect:/admin_book";
    }

    @NeedLogin
    @RequestMapping("/add_book")
    public String addBook(@RequestParam("bname") String bookName,
                          @RequestParam("bauthor") String bookAuthor,
                          @RequestParam("bnum") int bookNum,
                          @RequestParam("bbrief") String bookBrief,
                          @RequestParam("burl") String bookUrl) {
        Book book = new Book();
        book.setName(bookName);
        book.setAuthor(bookAuthor);
        book.setBrief(bookBrief);
        book.setNum(bookNum);
        book.setId(bookMapper.selectMaxId()+1);
        book.setPicture(bookUrl);
        bookMapper.insert(book);
        return "redirect:/admin_book";
    }

    @NeedLogin
    @RequestMapping("/delete_book")
    public String adjustBook(@RequestParam("bid") int bookId) {
        bookMapper.deleteByPrimaryKey(bookId);
        return "redirect:/admin_book";
    }

    @NeedLogin
    @RequestMapping("/add_user")
    public String addUser(@RequestParam("uphone") long userPhone,
                          @RequestParam("uname") String userName,
                          @RequestParam("upassword") String userPassword,
                          @RequestParam("uauthority") int userAuthority){
        User user = new User();
        user.setPhone(userPhone);
        if(userMapper.selectByPrimaryKey(userPhone)!=null){
            return "redirect:/admin_user";
        }
        user.setName(userName);
        user.setPassword(userPassword);
        user.setAuthority(userAuthority);
        userMapper.insert(user);
        return "redirect:/admin_user";
    }

    @NeedLogin
    @RequestMapping("/adjust_user")
    public String adjustUser(@RequestParam("uphone") long userPhone,
                             @RequestParam("uname") String userName,
                             @RequestParam("upassword") String userPassword,
                             @RequestParam("uauthority") int userAuthority){
        User user = new User();
        user.setPhone(userPhone);
        user.setName(userName);
        user.setPassword(userPassword);
        user.setAuthority(userAuthority);
        userMapper.updateByPrimaryKey(user);
        return "redirect:/admin_user";
    }

    @NeedLogin
    @RequestMapping("/delete_user")
    public String deleteUser(@RequestParam("uphone") long userPhone){
        userMapper.deleteByPrimaryKey(userPhone);
        return "redirect:/admin_user";
    }

    @NeedLogin
    @RequestMapping("/admin_user")
    public String adminUser(ModelMap modelMap,HttpServletRequest request){
        User user = (User)request.getSession().getAttribute("user");
        int authority = user.getAuthority();
        if(authority<2){
            return "redirect:/admin_book";
        }
        List<User> users = userMapper.selectAll();
        modelMap.addAttribute("users",users);
        return "admin_user";
    }
}
